import validate from 'wechat-validate'

Page({
  behaviors: [validate],
  data: {
    // #region form data
    houseId: '',
    name: '',
    gender: 1,
    mobile: '',
    visitDate: '',
    // #endregion

    houseName: '',
    // dateLayerVisible: false,
    minDate: Date.now(),
    maxDate: Date.now() + 3 * 24 * 60 * 60 * 1000,
    // houseLayerVisible: false,
    // houseList: [
    //   { name: '北京西三旗花园1号楼 101' },
    //   { name: '北京东村家园3号楼 302' },
    //   { name: '北京育新花园3号楼 703' },
    //   { name: '北京天通苑北苑8号楼 403' },
    // ],
  },

  rules: {
    houseId: [{ required: true, message: '请选择到访的房屋!' }],
    name: [
      { required: true, message: '访客姓名不能为空!' },
      { pattern: /[\u4e00-\u9fa5]{2,5}/, message: '访客姓名只能为中文!' },
    ],
    mobile: [
      { required: true, message: '访客手机号不能为空!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!' },
    ],
    visitDate: [{ required: true, message: '请选择到访的日期!' }],
  },

  // onConfirmDateTimePicker(e) {
  //   console.log('[ e ] >', e)
  //   this.setData({
  //     visitDate: wx.utils.formatDateToString(e.detail),
  //     dateLayerVisible: false,
  //   })
  // },

  // onSelectedHouse(e) {
  //   const { id, name } = e.detail
  //   this.setData({
  //     houseId: id,
  //     houseName: name,
  //   })
  // },
  // openHouseLayer() {
  //   this.setData({ houseLayerVisible: true })
  // },
  // closeHouseLayer() {
  //   this.setData({ houseLayerVisible: false })
  // },
  // openDateLayer() {
  //   this.setData({ dateLayerVisible: true })
  // },
  // closeDateLayer() {
  //   this.setData({ dateLayerVisible: false })
  // },
  async goPassport() {
    if (!this.validate()) return

    const { houseId, name, gender, mobile, visitDate } = this.data

    const res = await wx.http({
      method: 'POST',
      url: '/visitor',
      data: { houseId, name, gender, mobile, visitDate },
    })

    wx.utils.toast('填写成功')

    wx.reLaunch({
      url: '/visitor_pkg/pages/passport/index?id=' + res.data.id,
    })
  },
})
